package com.weilai.common.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weilai.common.model.SysUser;
import com.weilai.common.service.SysUserService;
import com.weilai.common.utils.StringRandom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Song on 2017/6/3.
 */
@Controller
@RequestMapping("/sysUser")
public class SysUserController {
    @Autowired
    SysUserService sysUserService;

    /**
     * 登陆
     * @param model
     * @param sysUser
     * @param requests
     * @return
     */
    @RequestMapping("findByUser")
    public String findByUser(Model model, @ModelAttribute SysUser sysUser, HttpServletRequest requests){

        SysUser sysUser1 = sysUserService.findByUser(sysUser);
        //userType 不为空时说明是管理员登陆
            if(sysUser1 != null){
                requests.getSession().setAttribute("sysUser",sysUser1);
                System.out.println("用户：************"+sysUser.getName());
                return "houtai/main";
            }else{
                return "redirect:/adminLogin.jsp";
            }
    }

    /**
     * 前台用户登录
     * @param request
     * @return
     */
    @RequestMapping(value = "frontFindByUser",method = {RequestMethod.POST},consumes="application/json")
    @ResponseBody
    public Integer frontFindByUser(@RequestBody ModelMap map, HttpServletRequest request ){
        SysUser sysUser = new SysUser();
        sysUserService.addSysUser(sysUser);
        sysUser.setMobileNumber((String)map.get("mobileNumber"));
        if((map.get("code")).equals(request.getSession().getAttribute("stringRandom"))){
            SysUser user = sysUserService.frontFindByUser(sysUser);
            if(user!=null){
                request.getSession().setAttribute("user",user);
                return 1;
            }else{
                return 0;
            }
        }else{
            //返回2 代表验证码错误
            return 2;
        }
    }

    /**
     * 退出
     * @param request
     * @return
     */
    @RequestMapping("logOut")
    public String logOut(HttpServletRequest request){
        request.getSession().invalidate();
        return "redirect:/adminLogin.jsp";
    }


    /**
     *
     * 添加用户
     * @param
     */
    @RequestMapping(value = "addSysUser")
    private String  addSysUser(@ModelAttribute SysUser sysUser,Model model){
        //0 表示所添加的用户为普通用户
        sysUser.setUserType("0");
        if (sysUserService.addSysUser(sysUser)== 1){
            model.addAttribute("httpCode","200");
        }else{
            model.addAttribute("httpCode","400");
        }
        return "redirect:/sysUser/find";
    }

    /**
     * 前台用户注册
     * @param sysUser
     * @param model
     * @return
     */
    @RequestMapping(value = "frontAddSysUser",method = {RequestMethod.POST})
    @ResponseBody
    private void  frontAddSysUser(@RequestBody SysUser sysUser,Model model){
        //0 表示所添加的用户为普通用户
        sysUser.setUserType("0");
        if (sysUserService.addSysUser(sysUser)== 1){
            model.addAttribute("httpCode","200");
        }else{
            model.addAttribute("httpCode","400");
        }
    }

    /**
     * 跳转到添加界面
     * @return
     */
    @RequestMapping("toAddSysUser")
    public String toAddSysUser(){
        return "houtai/add_user";
    }

    /**
     * 跳转到修改界面
     * @return
     */
    @RequestMapping("/toUpdateUser")
    public String toUpdateUser(Model model, SysUser sysUser){
        SysUser byUser = sysUserService.selectByPrimaryKey(sysUser.getId());
        model.addAttribute("user",byUser);
        return "houtai/update_user";
    }

    /**
     * 跳转到用户操作界面
     * @return
     */
    @RequestMapping("toOptionSysUser")
    public String toOptionSysUser(){
        return "houtai/user_operation";
    }

    /**
     * 前台修改密码
     * @param sysUser
     * @param model
     * @return
     */
    @RequestMapping(value = "frontUpdatePwd",method = {RequestMethod.POST})
    @ResponseBody
    private String  frontUpdatePwd(@RequestBody SysUser sysUser,Model model){
        if (sysUserService.updateBymobilenumberSelective(sysUser)== 1){
            model.addAttribute("httpCode","200");
        }else{
            model.addAttribute("httpCode","400");
        }
        return "修改成功";
    }

    /**
     * 删除用户
     * @param sysUserId
     * @return
     */
    @RequestMapping("delSysUser")
    public String  delSysUser(Model model,@RequestParam String sysUserId ){
        if (sysUserService.delSysUser(sysUserId)== 1){
            model.addAttribute("httpCode","200");
//            model.addAttribute("code", StringRandom.getStringRandom(8));
        }else{
            model.addAttribute("httpCode","400");
        }
        return "redirect:/sysUser/find";
    }

  /**
     * 修改
     * @param sysUser
     * @return
     */
    @RequestMapping("updateUser")
    public String  updateUser(Model model,@ModelAttribute SysUser sysUser ){
        if (sysUserService.updateByIdSelective(sysUser)== 1){
            model.addAttribute("httpCode","200");
            model.addAttribute("code", StringRandom.getStringRandom(8));
        }else{
            model.addAttribute("httpCode","400");
        }
        return "redirect:/sysUser/find";
    }


    /**
     * 查找所有用户的奖章数量
     * @return
     */
    @RequestMapping(value = "findAllOfMedal",method = RequestMethod.GET)
    public String  findAllOfMedal(Model model){
        SysUser allOfMedal = sysUserService.findAllOfMedal();
        return "";
    }

    @RequestMapping(value = "find",method = RequestMethod.GET)
    public String find(Model model,@RequestParam(defaultValue = "1",required = false) int pageNum,
    @RequestParam(defaultValue = "10",required = false) int pageSize){

        //赋值页面页码大小自动帮我们计算，我们只需要在页面上使用PageInfo的数据就行
        PageHelper.startPage(pageNum, pageSize);
        Page<SysUser> sysUsers = (Page) sysUserService.find();
        //分页信息
        PageInfo<SysUser> pageInfo = new PageInfo(sysUsers);
        model.addAttribute("page",pageInfo);


//        List<SysUser> sysUsers = sysUserService.find();
//        model.addAttribute("sysUsers",sysUsers);
//        for (SysUser sys: sysUsers) {
//            System.out.println("///////////////"+"age:"+sys.getAge()+""+sys.getWeixinNumber()+sys.getMobileNumber());
//        }
        return "houtai/user_operation";
    }




    //后台main.jsp的资源界面
    @RequestMapping("/top")
    public String top(){;return "houtai/top";}
    @RequestMapping("/left")
    public String left(){return "houtai/left";}
    @RequestMapping("/index1")
    public String index1(){return "houtai/index";}
}



